<?php

class Jf_View_Block_Tree_Menu extends Jf_View_Block_Tree_Abstract {

    const EOL = "\n";

    protected $_ulClass;

    public function menu (Jf_Tree_Container $container = null, $options = null) {
        if (null !== $container) {
            $this -> setContainer ($container);
        }
        if (null !== $options) {
            foreach ($options as $key => $value) {
                $method = 'set' . ucfirst ($key);
                $this -> $method ($value);
            }
        }
        return $this;
    }

    public function renderMenu () {
        $container = $this -> getContainer ();
        $minDepth = $this -> getMinDepth ();
        $maxDepth = $this -> getMaxDepth ();
        $found = $this -> findActive ($container, $minDepth, $maxDepth);
        if ($found) {
            $foundNode = $found['node'];
            $foundDepth = $found['depth'];
        } else {
            $foundNode = null;
        }
        // create iterator
        $iterator = new RecursiveIteratorIterator ($container, RecursiveIteratorIterator::SELF_FIRST);
        if (is_int ($maxDepth)) {
            $iterator -> setMaxDepth ($maxDepth);
        }
        // iterate container
        $prevDepth =  - 1;
        $indent = '';
        $html = '';
        $ulClass = $this -> getUlClass ();
        foreach ($iterator as $node) {
            $depth = $iterator -> getDepth ();
            $depth -= 0;
            $myIndent = $indent . str_repeat ('        ', $depth);
            if ($depth < $minDepth ||  ! $this -> accept ($node)) {
                continue;
            }
            if ($depth > $prevDepth) {
                if ($ulClass && $depth === 0) {
                    $ulClass = ' class="' . $ulClass . '"';
                } else {
                    $ulClass = '';
                }
                $html .= $myIndent . '<ul' . $ulClass . '>' . self::EOL;
            } else 
                if ($prevDepth > $depth) {
                    for ($i = $prevDepth; $i > $depth; $i -- ) {
                        $ind = $indent . str_repeat ('        ', $i);
                        $html .= $ind . '    </li>' . "\n";
                        $html .= $ind . '</ul>' . "\n";
                    }
                    $html .= $myIndent . '    </li>' . "\n";
                } else {
                    $html .= $myIndent . '    </li>' . "\n";
                }
            $liClass = $node -> isActive (true) ? 'class="active"' : '';
            $html .= $myIndent . '    <li ' . $liClass . '>' . "\n" . $myIndent . '        ';
            if ($depth < $maxDepth && $node -> hasNodes ()) {
                $html .= "<span>$node->label</span>" . "\n";
            } elseif ($depth == $minDepth &&  ! $node -> hasNodes ()) {
                $html .= "<a href=\"$node->href\"><span>$node->label</span></a>" . "\n";
            } else {
                $html .= "<a href=\"$node->href\"><span>$node->label</span></a>" . "\n";
            }
            $prevDepth = $depth;
        }
        for ($i = $prevDepth + 1; $i > 0; $i -- ) {
            $myIndent = $indent . str_repeat ('        ', $i - 1);
            $html .= $myIndent . '    </li>' . "\n" . $myIndent . '</ul>' . "\n";
        }
        return $html;
    }

    public function setUlClass ($ulClass) {
        if (is_string ($ulClass)) {
            $this -> _ulClass = $ulClass;
        }
        return $this;
    }

    public function getUlClass () {
        return $this -> _ulClass;
    }

    public function render () {
        return $this -> renderMenu ();
    }

}
